const router = require('koa-router')();
const Users = require('../models/user.js')
const Goods = require('../models/goods.js')
/* 父路由 */
router.prefix('/users')
/* 子路由 */
router.get('/', async function (ctx) {
  var data = await Users.find({});
  console.log(data);
  ctx.body = {
    code: 200,
    msg: "请求成功",
    total: data.length,
    result: data
  };
});
router.post('/addCart', async ctx => {
  var  userId = ctx.cookies.get('userId');
  if(userId){
      var { productId } = ctx.request.body;
      console.log(productId);
      var data = await Goods.findOne({ productId: productId });
    
    
      var obj = JSON.parse(JSON.stringify(data));
      obj['checked'] = true;
      obj['productNum'] = 1;
      console.log(obj);
    
      var userData = await Users.findOne({});
      if (userData.cartList.every(item => item.productId != productId)) {
        await Users.update({ userId: userId }, { $push: { 'cartList': obj } });
        ctx.body = {
          code: 200,
          msg: "添加成功",
        };
      } else {
        ctx.body = {
          code: 300,
          msg: "已经添加到购物车",
        };
      }
  }else{
    ctx.body = {
      msg:'没有登录'
    }
  }


})
/* 登录 */
router.post('/login',async ctx=>{
  var data = ctx.request.body;
  console.log(data);
  var res = await Users.findOne(data);
  console.log(res);
  if(res){
    ctx.cookies.set("userId",res.userId,{
      maxAge:1000*60*60
    });
    ctx.cookies.set('userName',res.userName,{
      maxAge:1000*60*60
    })
    ctx.body = {
      code:200,
      msg:"登录成功",
      userName:res.userName
    }
  }else{
    ctx.body = {
      code:400,
      msg:'用户名或密码错误'
    }
  }
});

/* 退出 */
router.post('/logout',async ctx=>{
  ctx.cookies.set("userId","",{
    maxAge:-1
  });
  ctx.cookies.set("userName","",{
    maxAge:-1
  });
  ctx.body = {
    code:200,
    msg:"退出登录"
  }
})

/* 检查登录状态 */
router.get('/checkLogin',async ctx=>{
  var userId = ctx.cookies.get('userId');
  console.log(userId);
  if(userId){
    ctx.body = {
      code:200,
      msg:'登录成功',
      result:ctx.cookies.get('userName')
    }
  }else{
    ctx.body = {
      code:1001,
      msg:'未登录'
    }
  }
})

/* 查询购物车 */
router.post('/cartlist',async ctx=>{
  var data = ctx.request.body;
  var res = await Users.findOne(data);

  console.log(res);
  if(res){
    ctx.body = {
      code:200,
      result:res.cartList,
      msg:"请求成功"
    }
  }else{
    ctx.body = {
      code:1001,
      msg:"请求失败"
    }
  }
})

/* 删除商品 */
router.post('/delCart',async ctx=>{
  var data = ctx.request.body;
  var userId = ctx.cookies.get('userId');
  console.log(userId);
  var res = await Users.update({userId:userId},{"$pull":{"cartList":data}});
  console.log(res.ok);
  ctx.body = {
    res
  }
})

router.post('/updateCart',async ctx=>{
  
  var {productNum,productId,checked} = ctx.request.body;
  var userId = ctx.cookies.get('userId');
  var res = await Users.updateOne({userId:userId,"cartList.productId":productId},
  {$set:{"cartList.$.productNum":Number(productNum),"cartList.$.checked":Boolean(checked)}});
  console.log(res);
  if(res.n!=0){
    ctx.body = {
      code:200,
      msg:'修改成功'
    }
  }else{
    ctx.body = {
      code:400,
      msg:'修改失败'
    }
  }
})

module.exports = router
